package cn.demo.sys.service.impl;

import cn.demo.common.core.page.PageQuery;
import cn.demo.common.utils.ArrayUtils;
import cn.demo.common.utils.DateUtils;
import cn.demo.common.utils.ObjUtils;
import cn.demo.sys.domain.SysOperLog;
import cn.demo.sys.mapper.SysOperLogMapper;
import cn.demo.sys.service.ISysOperLogService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper,SysOperLog> implements ISysOperLogService {
    @Resource
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        operLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */

    @Override
    public Page<SysOperLog> selectOperLogPage(SysOperLog operLog, PageQuery pageQuery) {
        return operLogMapper.paginate(pageQuery.build(), getQueryWrapper(operLog));
    }
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        return operLogMapper.selectListByQuery(getQueryWrapper(operLog));
    }

    private static QueryWrapper getQueryWrapper(SysOperLog operLog) {
        return QueryWrapper.create(operLog, SysOperLog.operators)
                .in(SysOperLog::getBusinessType, operLog.getBusinessTypes(), ArrayUtils.isNotEmpty(operLog.getBusinessTypes()))
                .between(SysOperLog::getOperTime, DateUtils.beginOfDay(operLog.getParams().get("beginTime")), DateUtils.endOfDay(operLog.getParams().get("endTime")), ObjUtils.isNotEmpty(operLog.getParams().get("beginTime")) && ObjUtils.isNotEmpty(operLog.getParams().get("beginTime")));
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(List<String> operIds) {
        return operLogMapper.deleteBatchByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(String operId) {
        return operLogMapper.selectOneById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }
}
